<?php

if ( !defined('T_ML_COMMENT') )
{
	define('T_ML_COMMENT', T_COMMENT);
}

if ( !defined('T_DOC_COMMENT') )
{
    define('T_DOC_COMMENT', T_ML_COMMENT);
}

/**
 * Md5CheckSum
 *
 * @package		Md5CheckSum
 * @version		$Revision: 1 $
 * @date 		$Date: 2007-09-05 21:15:02 +0100 (Wed, 05 Sep 2007) $
 * @author		Raymond Benc (natio) <www.raybenc.com>
 * @copyright   phpFoX
 */
class Md5CheckSum
{
	/**
	 * Display HTML
	 *
	 * @access	public
	 *
	 * @var string
	 */
	var $sDisplay;

	/**
	 * Strip out extra whitespace
	 *
	 * @access	private
	 *
	 * @var bool
	 */
	var $bTrimArray = false;

	var $aFiles = array();

	/**
	 * Get MD5 check sum for all the files in a directory
	 *
	 * @access	public
	 *
	 * @param	string	$sFromDir	is the full directory path
	 * @param	array	$aPost	is the optional post data
	 *
	 */
	function GetSums($sFromDir, $aPost = array(), $bReturn = false)
	{
		static $iCnt = 0;

		if ( !defined('sName') )
		{
			exit('Please be sure to define the script name with the constant: <b>sName</b>');
		}

		$aFiles = array();

		$aTempDisAllowed = explode(",", $aPost['disallowed']);
		$aInvalid = array();
		foreach($aTempDisAllowed as $sNotAllowed)
		{
			$aInvalid[] = rtrim(trim($sNotAllowed), "/");
		}

		$mHandle = opendir($sFromDir);

		while (false !== ( $sItem = readdir($mHandle) ))
		{
			$sFrom = str_replace('//', '/', $sFromDir .'/'.$sItem);
			$newfrom = str_replace($aPost['directory'] . "/", "", $sFrom);

			if ( $sItem == '.' || $sItem == '..' )
			{
				continue;
			}

			if ( in_array($newfrom, $aInvalid) )
	       	{
	       		continue;
	       	}

			if ( !in_array($newfrom, $aInvalid) )
	       	{
				if ( is_file($sFrom) || is_dir($sFrom) )
	           	{
					if ( !eregi(sName, $sFrom) )
					{
				 		$aAllowedExtensions = explode(",", $aPost['extensions']);

				 		foreach($aAllowedExtensions as $sExtension)
				 		{
				 			$sExtension = trim($sExtension);

				 			if ( substr($newfrom, "-". strlen($sExtension) ."") == $sExtension )
				 			{
				 				if ( !in_array($newfrom, $aInvalid) )
				 				{
				 					$aFiles[] = $sFrom;
		 						}
				 			}
				 		}
				 	}
				}

       			if (is_dir($sFrom))
        		{
        			$this->GetSums($sFrom, $aPost, $bReturn);
        		}
        	}
		}

		closedir($mHandle);

		foreach($aFiles as $aFile)
		{
			if ( file_exists($aFile) )
			{
				$iCnt++;
				if ( $bReturn )
				{
					$this->aFiles[] = $aFile;
				}
				else
				{
					$this->sDisplay .= ($this->bTrimArray ? '' : "\t") .''. ($iCnt != 1 ? ',' : '') .'\''. str_replace(rtrim($aPost['directory'], "/") . "/", "", $aFile) .'\' => \''. $this->GetFileHash($aFile) .'\'' . ($this->bTrimArray ? '' : "\n");
				}
			}
		}
	}

	/**
	 * Get the file has by adding MD5()
	 *
	 * @access	private
	 *
	 * @param	string	$sFile	is the name of the file
	 *
	 * @return	string	Hashed value
	 */
	function GetFileHash($sFile)
	{
		return md5(str_replace("\r\n", "\n", file_get_contents($sFile)));
	}

	/**
	 * Saving the file by sending it to the user so they can
	 * download it on the fly
	 *
	 * @access	public
	 *
	 * @param	string	$sArrayName	is the name of the array variable
	 * @param	string	$sFileName	is the name of the PHP md5 hash file
	  */
	function SaveSum($sArrayName = 'md5sum', $sFileName = 'md5checksum.php', $bReturn = false)
	{
		$sStr = "<?php\n";
		$sStr .= "/**\n * MD5 CheckSum created by MD5CheckSum (www.md5checksum.org) | natio (www.raybenc.com)\n";
		$sStr .= ' * @version $Id: md5CheckSum.class.php 270 2008-02-01 16:11:42Z phpfoxsv $' . "\n";
 		$sStr .= " */\n\n";
		$sStr .='$'. $sArrayName .' = array(' . "\n";
		$sStr .= $this->sDisplay;
		$sStr .= "\n);\n?>";
		if ( $bReturn )
		{
			return $sStr;
		}
		@ob_clean();
		ob_start();
		@ob_clean();
		header('Content-disposition: attachment; filename='. $sFileName .'');
		header('Content-type: unknown/unknown');
		print $sStr;
		exit;
	}

	/**
	 * Check any modified files
	 *
	 * @access public
	 *
	 * @param	array	$aCheckSum is the check sum array we got from self::GetSums earlier
	 * @param	string	$sDir	is the full directory path of where the files are located
	 * @param	array	$aDisallow	is an optional array of files we should not include in the check
	 *
	 * @return	array	List of modified files; if any
	 */
	function CheckModifiedFiles($aCheckSum, $sDir, $aDisallow = array())
	{
		$aModified = array();
		foreach($aCheckSum as $sFileName => $sHash)
		{
			$sFile = $sDir . $sFileName;
		 	if ( file_exists($sFile) && is_file($sFile) && ($aDisallow && !in_array($sFileName, $aDisallow)) )
 			{
 				if ( $this->GetFileHash($sFile) != $sHash )
 				{
 					$aModified[] = array('file' => $sFileName, 'time' => filemtime($sFile));
 				}
 			}
 		}
 		return $aModified;
	}
}

?>